package com.snow.useviewdemo.util;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.Signature;

import androidx.core.text.TextUtilsCompat;
import androidx.core.view.ViewCompat;

import java.util.Locale;

import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;

/**
 * author : zyt
 * e-mail : 632105276@qq.com
 * date   : 2020-04-09
 * desc   :
 */
public class UAppUtils {
    private static UAppUtils appUtils;

    public static UAppUtils init() {
        if (appUtils == null) {
            synchronized (UAppUtils.class) {
                if (appUtils == null) {
                    appUtils = new UAppUtils();
                }
            }
        }
        return appUtils;
    }

    /**
     * 获取应用的签名值===防止反编译二次打包使用
     *
     * @param application
     * @return
     */
    public String getAppSign(Context application) {
        try {
            PackageInfo packageInfo = application.getPackageManager().getPackageInfo(application.getPackageName(), PackageManager.GET_SIGNATURES);
            Signature[] signatures = packageInfo.signatures;
            StringBuilder builder = new StringBuilder();
            for (Signature signature : signatures) {
                builder.append(signature.toCharsString());
            }
            return builder.toString();
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 是否是rtl
     *
     * @return
     */
    public boolean isRtl() {
        return TextUtilsCompat.getLayoutDirectionFromLocale(Locale.getDefault()) == ViewCompat.LAYOUT_DIRECTION_RTL;
    }

    /**
     * 模拟按下home键，返回桌面
     *
     * @param context
     */
    public static void gotoHomeActivity(Context context) {
        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.setFlags(FLAG_ACTIVITY_NEW_TASK);
        intent.addCategory(Intent.CATEGORY_HOME);
        context.startActivity(intent);
    }
}
